home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / program / 332 / tools / size.c < prev   
Encoding:
C/C++ Source or Header  |  1988-10-19  |  3.6 KB  |  190 lines

  1. /* Copyright (c) 1988 by Sozobon, Limited.  Author: Tony Andrews
  2.  *
  3.  * Permission is granted to anyone to use this software for any purpose
  4.  * on any computer system, and to redistribute it freely, with the
  5.  * following restrictions:
  6.  * 1) No charge may be made other than reasonable charges for reproduction.
  7.  * 2) Modified versions must be clearly marked as such.
  8.  * 3) The authors are not responsible for any harmful consequences
  9.  *    of using this software, even if they result from defects in it.
  10.  */
  11.  
  12. static    char    Version[] =
  13. "size: version 1.01  Copyright (c) 1988 by Sozobon, Limited.";
  14.  
  15. #include <stdio.h>
  16. #include <fcntl.h>
  17. #include <ar.h>
  18.  
  19. /*
  20.  * Object file header
  21.  */
  22. struct hdr {
  23.     int    magic;
  24.     long    tsize;
  25.     long    dsize;
  26.     long    bsize;
  27.     long    ssize;
  28.     long    stksize;
  29.     long    entrypt;
  30.     int    rlbflg;
  31. };
  32.  
  33. #define    OMAGIC    0x601a        /* object module magic number */
  34.  
  35. extern    long    lseek();
  36.  
  37. int    oflag = 0;
  38. int    xflag = 0;
  39.  
  40. usage()
  41. {
  42.     fprintf(stderr, "usage: size [-o|-x] [-V] file ...\n");
  43.     exit(1);
  44. }
  45.  
  46. main(argc, argv)
  47. int    argc;
  48. char    *argv[];
  49. {
  50.     for (argv++; *argv != NULL && argv[0][0] == '-' ;argv++) {
  51.         switch (argv[0][1]) {
  52.  
  53.         case 'o':
  54.             oflag++; break;
  55.         case 'x':
  56.             xflag++; break;
  57.         case 'V':
  58.         case 'v':
  59.             printf("%s\n", Version);
  60.             break;
  61.         default:
  62.             usage();
  63.         }
  64.     }
  65.  
  66.     for (; *argv != NULL; argv++)
  67.         dofile(*argv);
  68.  
  69.     exit(0);
  70. }
  71.  
  72. dofile(f)
  73. char    *f;
  74. {
  75.     int    fd;
  76.     struct    hdr    h;
  77.     int    magic;
  78.  
  79.     if ((fd = open(f, O_RDONLY)) < 0) {
  80.         fprintf(stderr, "size: can't open file '%s'\n", f);
  81.         return;
  82.     }
  83.  
  84.     if (read(fd, &magic, 2) != 2) {
  85.         fprintf(stderr, "size: '%s' not an object module\n", f);
  86.         close(fd);
  87.         return;
  88.     }
  89.  
  90.     switch (magic) {
  91.  
  92.     case OMAGIC:    /* object module */
  93.         lseek(fd, 0L, 0);
  94.         if (read(fd, &h, sizeof(h)) != sizeof(h)) {
  95.             fprintf(stderr, "size: '%s' not an object module\n", f);
  96.             close(fd);
  97.             return;
  98.         }
  99.         showit(NULL, f, &h);
  100.         break;
  101.  
  102.     case ARMAG1:    /* libraries */
  103.     case ARMAG2:
  104.         dolib(f, fd);
  105.         break;
  106.     }
  107.  
  108.     close(fd);
  109. }
  110.  
  111. /*
  112.  * gethdr(fd, a) - read an archive header at the current file position
  113.  *
  114.  * Returns TRUE on success, FALSE at eof.
  115.  */
  116. int
  117. gethdr(fd, a)
  118. int    fd;
  119. struct    ar_hdr    *a;
  120. {
  121.     if (read(fd, a, ARHSZ) == ARHSZ)
  122.         return (a->ar_name[0] != '\0');
  123.     else
  124.         return 0;
  125. }
  126.  
  127. /*
  128.  * dolib(f, fd) - show the size of all object modules in a library
  129.  */
  130. dolib(f, fd)
  131. char    *f;
  132. int    fd;
  133. {
  134.     struct    ar_hdr    a;
  135.     struct    hdr    h;
  136.     long    next;
  137.     long    ts, ds, bs;
  138.  
  139.     ts = ds = bs = 0;
  140.  
  141.     while (gethdr(fd, &a)) {
  142.         next = lseek(fd, 0L, 1) + a.ar_size;
  143.         if (read(fd, &h, sizeof(h)) != sizeof(h) || h.magic != OMAGIC) {
  144.             fprintf(stderr, "size: '%s' not an object module\n", f);
  145.             break;
  146.         }
  147.         ts += h.tsize;
  148.         ds += h.dsize;
  149.         bs += h.bsize;
  150.  
  151.         showit(f, a.ar_name, &h);
  152.  
  153.         if (lseek(fd, next, 0) < 0)
  154.             break;
  155.     }
  156.  
  157.     printf("%s(TOTAL): ", f);
  158.  
  159.     if (oflag)
  160.         printf("%lo + %lo + %lo = %lo\n", ts, ds, bs, ts+ds+bs);
  161.     else if (xflag)
  162.         printf("0x%lx + 0x%lx + 0x%lx = 0x%lx\n", ts, ds, bs, ts+ds+bs);
  163.     else /* decimal */
  164.         printf("%ld + %ld + %ld = %ld\n", ts, ds, bs, ts+ds+bs);
  165. }
  166.  
  167. showit(l, f, h)
  168. char    *l;
  169. char    *f;
  170. struct    hdr    *h;
  171. {
  172.     if (l != NULL)
  173.         printf("%s(%.14s): ", l, f);
  174.     else
  175.         printf("%.14s: ", f);
  176.  
  177.     if (oflag)
  178.         printf("%lo + %lo + %lo = %lo\n",
  179.             h->tsize, h->dsize, h->bsize,
  180.             h->tsize + h->dsize + h->bsize);
  181.     else if (xflag)
  182.         printf("0x%lx + 0x%lx + 0x%lx = 0x%lx\n",
  183.             h->tsize, h->dsize, h->bsize,
  184.             h->tsize + h->dsize + h->bsize);
  185.     else /* decimal */
  186.         printf("%ld + %ld + %ld = %ld\n",
  187.             h->tsize, h->dsize, h->bsize,
  188.             h->tsize + h->dsize + h->bsize);
  189. }
  190.